package com.online_vote_system.demo.controller;

import com.online_vote_system.demo.service.AudienceService;
import com.online_vote_system.demo.service.CompetitionService;
import com.online_vote_system.demo.vo.Audience;
import com.online_vote_system.demo.vo.Competition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;
@Slf4j
@Controller
public class AudienceController {

    @Autowired
    AudienceService audienceService;
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CompetitionService competitionService;

    @GetMapping(value = {"/Audience","/"})
    public String Audience(){
        return "html/AudienceLogin";
    }
    @ResponseBody
    @GetMapping("/sendEmail")
    public Boolean sendEmail(String audEmail) throws MessagingException {

        // 构建一个邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        // 设置邮件主题
        mimeMessageHelper.setSubject("【投票】验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        mimeMessageHelper.setFrom("852201810@qq.com");
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        mimeMessageHelper.setTo(audEmail);
        // 设置邮件抄送人，可以有多个抄送人
//        message.setCc("12****32*qq.com");
        // 设置隐秘抄送人，可以有多个
//        message.setBcc("7******9@qq.com");
        // 设置邮件发送日期
        mimeMessageHelper.setSentDate(new Date());
        //设置验证码
        int tmp = (int)((Math.random()*9+1)*100000);
        // 设置邮件的正文
        mimeMessageHelper.setText("你的邮箱验证码为"+"<h1>" + tmp + "</h1>",true);//true为设置为html页面
        //在Redis中存储
        redisTemplate.opsForValue().set(audEmail,tmp,300, TimeUnit.SECONDS);//同时设置过期时间为300秒就是5分钟
        // 发送邮件
        javaMailSender.send(mimeMessage);
        return true;
    }

    @PostMapping("/testEmail")
    public String testEmail(String audEmail,
                            String emailTest,
                            Model model,
                            HttpServletRequest request){
            String result = String.valueOf(redisTemplate.opsForValue().get(audEmail));
            HttpSession session = request.getSession();
            if (emailTest.equals(result)){
                Audience audience = audienceService.getByEmail(audEmail);
                System.out.println(audience);
                if (audience == null){
                    audience = new Audience();
                    audience.setAudEmail(audEmail);
                    boolean e = audienceService.save(audience);
                }
                session.setAttribute("loginUser",audience);
                Competition competition = competitionService.getOneByType();
                if (competition == null){
                    return "html/Audience/noCompetition";
                }
                model.addAttribute("competition",competition);
                log.info(result+"::::"+emailTest+"::::"+audience.toString());
                return "html/Audience/index";
            }else {
                log.info(result+"::::"+emailTest);
                model.addAttribute("msg","验证码输入错误");
                return "html/AudienceLogin";
            }
    }


}
